package cn.fudan.pms.controller;

import cn.fudan.pms.domain.dto.EquipmentSearch;
import cn.fudan.pms.domain.vo.Page;
import cn.fudan.pms.mbg.mapper.JobMapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

import static cn.fudan.pms.json.JsonUtils.readFile;
import static cn.fudan.pms.json.JsonUtils.stringToJSONObject;

@Controller
@RequestMapping("job")
public class JobController {
    @Autowired
    private JobMapper jobMapper;

    private String str = readFile();
    private JSONObject fieldOfObjs = stringToJSONObject(str, "Attrs_zh");
    private Map<String, String> fieldOfObjMap = JSONObject.toJavaObject(fieldOfObjs, Map.class);

    /**
     * 调度员调用此接口
     * 将报修信息录入系统
     *
     * @param title
     * @param description
     * @param residentUuid
     * @param platform
     * @param reporterUuid 调度员
     */
    public void addNewJob(String title, String description, String residentUuid, String platform, String reporterUuid) {

    }


    /**
     * 调度员调用此接口
     * 将报修单分配给合适的维修工
     *
     * @param jobUuid
     */
    public void assignJob(String jobUuid, String operatorUuid, String reporterUuid) {

    }

    /**
     * @param jobUuid
     * @param subJobUuid
     */
    void handleJob(String jobUuid, String subJobUuid) {

    }

    @RequestMapping({"/index", "/"})
    public ModelAndView handleRequest() {
        ModelAndView mav = new ModelAndView("index/equipment");
        Page page = new Page();
        page.setPageIndex(Page.Index.EQUIPMENT);
        mav.addObject("page", page);
        mav.addObject("title", "设备管理");
        return mav;
    }

//    @RequestMapping("/inspect")
//    public ModelAndView inspect() {
//        ModelAndView mav = new ModelAndView("equipment/inspect");
//        List<Field> inspectFields = Arrays.asList(Equipment.class.getDeclaredFields());
//        mav.addObject("FOEMap", fieldOfObjMap);
//        mav.addObject("equipmentFields", inspectFields);
//        mav.addObject("equipmentList", jobMapper.listWaitForRepair());
//        Page page = new Page();
//        page.setPageIndex(Page.Index.EQUIPMENT);
//        mav.addObject("page", page);
//        mav.addObject("title", "待维修");
//        return mav;
//    }

    @RequestMapping("/repairRecord")
    public ModelAndView listRepairRecord() {
        EquipmentSearch equipmentSearch = new EquipmentSearch();
        equipmentSearch.setReportType("m");
        Date today = new Date();
        equipmentSearch.setEquipmentType("");
        equipmentSearch.setResidentID("");
        equipmentSearch.setFromMonthly(new java.sql.Date(today.getTime()));
        return searchRecord(equipmentSearch);
    }

    @RequestMapping(value = "/searchRepairRecord", method = RequestMethod.POST)
    public ModelAndView searchRecord(EquipmentSearch equipmentSearch) {
        ModelAndView mav = new ModelAndView("job/repairRecord");
//        List<Field> repairOrderFields = Arrays.asList(RowRepair.class.getDeclaredFields());
//        Date from = new Date(), to = new Date();
//        SearchDateUtil.getFromToBySearchType(from, to, equipmentSearch);
//        java.sql.Date sqlFrom = new java.sql.Date(from.getTime());
//        java.sql.Date sqlTo = new java.sql.Date(to.getTime());
//        List<RowRepair> orderList = jobMapper.listRepairOrderRecord(equipmentSearch.getResidentID(),
//                equipmentSearch.getEquipmentType(),
//                sqlFrom, sqlTo);
//        mav.addObject("FOEMap", fieldOfObjMap);
//        mav.addObject("equipmentFields", repairOrderFields);
//        mav.addObject("equipmentList", orderList);
//        double totalFee = 0;
//        for (RowRepair order : orderList) {
//            totalFee += order.getRepairFee();
//        }
//        mav.addObject("totalFee", totalFee);
//        Page page = new Page();
//        page.setPageIndex(Page.Index.EQUIPMENT);
//        mav.addObject("page", page);
//        mav.addObject("title", "报修记录");
        return mav;
    }
}
